package com.ee.chat.admin.service.impl;

import com.ee.chat.admin.service.ICallService;
import com.ee.chat.admin.service.IOperationService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.UserCallLogMapper;
import com.ee.chat.common.dao.UserToHostLogLableMapper;
import com.ndhutu.ee.util.DateUtil;
import com.ndhutu.ee.util.PageUtil;
import com.ndhutu.ee.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author Shane
 * @Date 2020/3/2 17:28
 */
@Service
public class CallServiceImpl implements ICallService {
    @Resource
    UserCallLogMapper userCallLogMapper;
    @Resource
    IOperationService operationService;
    @Resource
    UserToHostLogLableMapper userToHostLogLableMapper;

    @Override
    public Result list(HttpServletRequest request, Integer fromUserId, Integer toUserId, Integer callTimeMin, Integer callTimeMax,
                       Integer type, Integer vstatus, String time, String field, String sort, Integer page, Integer limit) throws Exception {
        Map<String, Object> data = new HashMap<>();

        if(StringUtil.isEmpty(time) && fromUserId == null && toUserId == null && callTimeMin == null
                && callTimeMax == null && type == null && vstatus == null){
            HttpSession session = request.getSession(true);
            List<String> userPermissions = (List<String>)session.getAttribute("userPermissions");
            if(!userPermissions.contains("/operate/call/viewList")){
                data.put("datas", new ArrayList<>());
                data.put("total", 0);

                return Result.returnSuccess(data);
            }
        }

        Date beginTime = null, endTime = null;
        if(!StringUtil.isEmpty(time)){
            String[] arr = time.split(" - ");
            beginTime = DateUtil.parse(arr[0]);
            endTime = DateUtil.parse(arr[1]);
        }

        List<Map<String, Object>> maps = userCallLogMapper.list(fromUserId, toUserId, callTimeMin, callTimeMax,
                type, vstatus,  beginTime, endTime, field, sort, PageUtil.getOffset(page, limit), limit);
        for(Map<String, Object>  map: maps){
            if(map.get("endTime") == null){
                map.put("callTime", "通话中");
            }else{
                String formatCallTime = DateUtil.formatTimeDiff(Long.parseLong(map.get("callTime").toString()));
                map.put("callTime", formatCallTime);
            }
            map.put("file", map.get("file") == null ? new String[0] : (map.get("file")+"").split(","));
            if(map.get("logId") != null){
                List<Map<String, Object>> lables =
                        userToHostLogLableMapper.queryHostLogLable(Integer.parseInt(map.get("logId").toString()));
                map.put("lables", lables);
            }
        }

        data.put("datas", maps);
        int total = userCallLogMapper.listCount(fromUserId, toUserId, callTimeMin, callTimeMax, type, vstatus, beginTime, endTime);
        data.put("total", total);

        operationService.addOperationLog(request, "获取通话记录数据");

        return Result.returnSuccess(data);
    }
}
